package com.zhoug.common3.utils;

import com.zhoug.common3.beans.ZDate;

import java.util.Calendar;
import java.util.Locale;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 正则表达式工具
 *
 * @Author: zhoug
 * @Date: 2023/8/19
 * @Description:
 */
public class RegexUtils {
    private static boolean DEBUG = true;
    private static final String TAG = ">>>RegexUtils";
    //年月日
    public static final String YYYY_MM_DD = "(\\d{4})[-\\.\\/年](0?[1-9]|1[0-2])[-\\.\\/月]([12]\\d|3[01]|0?[1-9])[-\\.\\/日号]?";
    //时分秒
    public static final String HH_MM_SS = "([01]?\\d|2[0-3])[:时点]([0-5]?\\d)[:分]([0-5]?\\d)[秒]?";
    //时分
    public static final String HH_MM = "([01]?\\d|2[0-3])[:时点]([0-5]?\\d)[:分]?";
    //年月日时分秒
    public static final String YYYY_MM_DD_HH_MM_SS = YYYY_MM_DD + "[T\\s]" + HH_MM_SS;
    //年月日时分
    public static final String YYYY_MM_DD_HH_MM = YYYY_MM_DD + "[T\\s]" + HH_MM;


    /**
     * 日期时间格式化输出 为 今天,一天前,2月18日
     *
     * @param time
     * @return
     */
    public static String formatTime(String time) {
        if (StringUtils.isNullOrEmpty(time)) return null;
        Pattern pattern = Pattern.compile(YYYY_MM_DD);
        if (DEBUG) {
            LogUtils.d(TAG, time + " matches:" + pattern.matcher(time).matches());
        }
        Matcher matcher = pattern.matcher(time);
        if (matcher.find()) {
            final String matcherStr = matcher.group(0);
            final String year = matcher.group(1);
            final String month = matcher.group(2);
            final String day = matcher.group(3);
            if (DEBUG) {
                LogUtils.d(TAG, String.format(Locale.CANADA, "匹配到日期:%s,年:%s,月:%s,日:%s", matcherStr, year, month, day));
            }
            //现在年月日
            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(System.currentTimeMillis());
            if (Objects.equals(year, getYear(calendar)) && Objects.equals(month, getMonth(calendar)) && Objects.equals(day, getDay(calendar))) {
                return "今天";
            } else {
                //是否是昨天
                calendar.add(Calendar.DAY_OF_YEAR, -1);
                if (Objects.equals(year, getYear(calendar)) && Objects.equals(month, getMonth(calendar)) && Objects.equals(day, getDay(calendar))) {
                    return "一天前";
                } else {
                    calendar.add(Calendar.DAY_OF_YEAR, +1);
                    //年度是今年只显示月日,否则显示年月日
                    String m = month.startsWith("0") ? month.substring(1) : month;
                    String d = day.startsWith("0") ? day.substring(1) : day;
                    if (Objects.equals(year, getYear(calendar))) {
                        return StringUtils.format("%s月%s日", m, d);
                    } else {
                        return StringUtils.format("%s年%s月%s日", year, m, d);
                    }

                }
            }
        }
        return null;
    }


    /**
     * 获取年
     *
     * @param calendar
     * @return
     */
    private static String getYear(Calendar calendar) {
        return String.valueOf(calendar.get(Calendar.YEAR));
    }

    /**
     * 获取月
     *
     * @param calendar
     * @return
     */
    private static String getMonth(Calendar calendar) {
        int m = calendar.get(Calendar.MONTH) + 1;
        return m >= 10 ? String.valueOf(m) : "0" + m;
    }

    /**
     * 获取日
     *
     * @param calendar
     * @return
     */
    private static String getDay(Calendar calendar) {
        int d = calendar.get(Calendar.DAY_OF_MONTH);
        return d >= 10 ? String.valueOf(d) : "0" + d;
    }

    /**
     * 字符串中获取年月日
     *
     * @param time
     * @return
     */
    public static String getDateYMD(String time) {
        Pattern pattern = Pattern.compile(YYYY_MM_DD);
        Matcher matcher = pattern.matcher(time);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    /**
     * 字符串中获取时分秒
     *
     * @param time
     * @return
     */
    public static String getDateHMS(String time) {
        Pattern pattern = Pattern.compile(HH_MM_SS);
        Matcher matcher = pattern.matcher(time);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    /**
     * 字符串中获取时分
     *
     * @param time
     * @return
     */
    public static String getDateHM(String time) {
        Pattern pattern = Pattern.compile(HH_MM);
        Matcher matcher = pattern.matcher(time);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }

    /**
     * 获取年月日时分秒
     * @param input
     * @return
     */
    public static ZDate findYYYY_MM_DD_HH_MM_SS(String input) {
        if (StringUtils.isNullOrEmpty(input)) return null;
        Pattern pattern = Pattern.compile(YYYY_MM_DD_HH_MM_SS);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            ZDate zDate = new ZDate();
            zDate.setDate(matcher.group(0));
            zDate.setYear(matcher.group(1));
            zDate.setMonth(matcher.group(2));
            zDate.setDay(matcher.group(3));
            zDate.setHour(matcher.group(4));
            zDate.setMinute(matcher.group(5));
            zDate.setSecond(matcher.group(6));
            return zDate;
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, input + " not find");
            }
        }
        return null;
    }


    /**
     * 获取年月日时分
     * @param input
     * @return
     */
    public static ZDate findYYYY_MM_DD_HH_MM(String input) {
        if (StringUtils.isNullOrEmpty(input)) return null;
        Pattern pattern = Pattern.compile(YYYY_MM_DD_HH_MM);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            ZDate zDate = new ZDate();
            zDate.setDate(matcher.group(0));
            zDate.setYear(matcher.group(1));
            zDate.setMonth(matcher.group(2));
            zDate.setDay(matcher.group(3));
            zDate.setHour(matcher.group(4));
            zDate.setMinute(matcher.group(5));
            return zDate;
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, input + " not find");
            }
        }
        return null;
    }


    /**
     * 获取年月日
     * @param input
     * @return
     */
    public static ZDate findYYYY_MM_DD(String input) {
        if (StringUtils.isNullOrEmpty(input)) return null;
        Pattern pattern = Pattern.compile(YYYY_MM_DD);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            ZDate zDate = new ZDate();
            zDate.setDate(matcher.group(0));
            zDate.setYear(matcher.group(1));
            zDate.setMonth(matcher.group(2));
            zDate.setDay(matcher.group(3));
            return zDate;
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, input + " not find");
            }
        }
        return null;
    }

    /**
     * 获取时分秒
     * @param input
     * @return
     */
    public static ZDate findHH_MM_SS(String input) {
        if (StringUtils.isNullOrEmpty(input)) return null;
        Pattern pattern = Pattern.compile(HH_MM_SS);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            ZDate zDate = new ZDate();
            zDate.setDate(matcher.group(0));
            zDate.setHour(matcher.group(1));
            zDate.setMinute(matcher.group(2));
            zDate.setSecond(matcher.group(3));
            return zDate;
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, input + " not find");
            }
        }
        return null;
    }

    /**
     * 获取时分
     * @param input
     * @return
     */
    public static ZDate findHH_MM(String input) {
        if (StringUtils.isNullOrEmpty(input)) return null;
        Pattern pattern = Pattern.compile(HH_MM);
        Matcher matcher = pattern.matcher(input);
        if (matcher.find()) {
            ZDate zDate = new ZDate();
            zDate.setDate(matcher.group(0));
            zDate.setHour(matcher.group(1));
            zDate.setMinute(matcher.group(2));
            return zDate;
        } else {
            if (DEBUG) {
                LogUtils.d(TAG, input + " not find");
            }
        }
        return null;
    }




}
